package org.sith.taskrunner.temp.tasks.wrappers;

import org.sith.taskrunner.api.task.Task;
import org.sith.taskrunner.api.task.TaskExecutionResult;
import org.sith.taskrunner.api.task.TaskInputData;
import org.sith.taskrunner.api.task.TaskWrapper;
import org.sith.taskrunner.api.exceptions.TaskExecutionException;
import org.sith.taskrunner.api.exceptions.TaskInitException;
import org.sith.taskrunner.api.impl.EmptyInputData;


public class MeasuringTaskExecutionTimeWrapper<T extends Task<? extends TaskInputData, O>, O extends TaskExecutionResult>
        implements TaskWrapper<T, EmptyInputData, MeasuringTaskExecutionTimeResult<O>> {

    private T task;

    @Override
    public void setTask(T task) {
        this.task = task;
    }

    @Override
    public void init(EmptyInputData inputData) throws TaskInitException {
    }

    @Override
    public MeasuringTaskExecutionTimeResult<O> executeTask() throws TaskExecutionException {
        long start = System.currentTimeMillis();
        O originalResult = task.executeTask();
        long finish = System.currentTimeMillis();

        TimeIntervalResult timeIntervalResult = new TimeIntervalResult(finish - start);

        MeasuringTaskExecutionTimeResult<O> result = new MeasuringTaskExecutionTimeResult<O>();
        result.setOriginalResult(originalResult);
        result.setResult(timeIntervalResult);
        return result;
    }
}
